package cn.edu.tju.elm.controller;

import cn.edu.tju.core.model.HttpResult;
import cn.edu.tju.core.model.ResultCodeEnum;
import cn.edu.tju.core.security.service.UserService;
import cn.edu.tju.elm.model.Business;
import cn.edu.tju.elm.service.BusinessService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/businesses")
@Tag(name="管理店铺", description = "提供对店铺的增删改查功能")
public class BusinessController {
    @Autowired
    private UserService userService;

    @Autowired
    private BusinessService businessService;

    @GetMapping("")
    @Operation(summary = "获取所有商家列表", description = "返回系统中所有商家的列表")
    public HttpResult<List<Business>> getBusinesses(){
        List<Business> businesses = businessService.getAllBusinesses();
        return HttpResult.success(businesses);
    }

    @PostMapping("")
    @PreAuthorize("hasAuthority('ADMIN')")
    @Operation(summary = "添加新商家", description = "添加一个新的商家信息，需要管理员权限")
    public HttpResult<Business> addBusiness(@RequestBody Business business){
        try {
            Business savedBusiness = businessService.saveBusiness(business);
            return HttpResult.success(savedBusiness);
        } catch (IllegalArgumentException e) {
            return HttpResult.failure(ResultCodeEnum.BAD_REQUEST, e.getMessage());
        } catch (Exception e) {
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR, "创建商家失败: " + e.getMessage());
        }
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取指定商家", description = "根据ID获取特定商家的详细信息")
    public HttpResult<Business> getBusiness(@PathVariable("id") Long id){
        Optional<Business> business = businessService.getBusinessById(id);
        if(business.isPresent()) {
            return HttpResult.success(business.get());
        } else {
            return HttpResult.failure(ResultCodeEnum.NOT_FOUND);
        }
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新商家信息", description = "全量更新指定商家的信息")
    public HttpResult<Business> updateBusiness(@PathVariable("id") Long id, @RequestBody Business business){
        Business updatedBusiness = businessService.updateBusiness(id, business);
        if(updatedBusiness != null) {
            return HttpResult.success(updatedBusiness);
        } else {
            return HttpResult.failure(ResultCodeEnum.NOT_FOUND);
        }
    }

    @PatchMapping("/{id}")
    @Operation(summary = "部分更新商家信息", description = "部分更新指定商家的信息")
    public HttpResult<Business> patchBusiness(@PathVariable("id") Long id, @RequestBody Business business){
        // 对于PATCH请求，我们也使用updateBusiness方法，因为我们的实现已经处理了部分更新的情况
        Business updatedBusiness = businessService.updateBusiness(id, business);
        if(updatedBusiness != null) {
            return HttpResult.success(updatedBusiness);
        } else {
            return HttpResult.failure(ResultCodeEnum.NOT_FOUND);
        }
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除商家", description = "删除指定ID的商家信息")
    public HttpResult<Business> deleteBusiness(@PathVariable("id") Long id){
        boolean deleted = businessService.deleteBusiness(id);
        if(deleted) {
            return HttpResult.success();
        } else {
            return HttpResult.failure(ResultCodeEnum.NOT_FOUND);
        }
    }
}
